package org.example.algo;

import java.util.Arrays;

/**
 * 选择排序
 * @author shenguangyang
 */
public class N01_SelectionSort {
    public static void main(String[] args) {
        int[] arr = {10, 1, 6, 9, 11, 0, 12, 5, 7, 8};
        selectSort(arr);
        System.out.println(Arrays.toString(Arrays.stream(arr).toArray()));
    }

    public static void selectSort(int[] arr) {
        if (arr == null || arr.length <= 1) {
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            int minValueIndex = i;
            for (int j = i; j < arr.length; j++) {
                minValueIndex = arr[j] <= arr[minValueIndex] ? j : minValueIndex;
            }
            swap(arr, minValueIndex, i);
        }
    }

    public static void swap(int[] arr, int v1Index, int v2Index) {
        int temp = arr[v1Index];
        arr[v1Index] = arr[v2Index];
        arr[v2Index] = temp;
    }
}
